
var dataBase = {
	seatMap : [],
};

//var selectSeats = []; //当前选中的座位

/*
思考一下逻辑，大部分应该是a，小部分是b或者d，应该采取替换的原则，而不能用status来直接设置状态(虽然这个方法比较简便)
*/

dataBase["seatMap"] = initSeatMap;

// 获取数据
var initData = {
	seatList : function(receiveData) {
		// 渲染座位图
		renderData.seatMap();
	}
}

// 渲染数据
var renderData = {
	seatMap : function(data) {
		var formatMap = initMapData();

		// var rows = $('#seat-rows').val();
		// var cols = $('#seat-cols').val();
		// var x = cols;
		// var y = rows;
		// var map = [];
		// for(var i=0;i<x;i++){
		// 	map[i]="";
		// 	for(var j=0;j<y;j++){
		// 		map[i]+="a";
		// 	}
		// }
		// dataBase["seatMap"] = map;

		var seatMap = dataBase["seatMap"];
		console.log('=====最终seatMap=====');
		console.log(seatMap);

		$('#seat-map').empty();
		$("#seat-map-legend").empty();

		var sc = $('#seat-map').seatCharts({
			map: seatMap,
			naming : {
				getLabel : function (character, row, column) {
					return '';
				}
			},
			seats: priceList,
			click: function () {
				if (this.status() == 'available') {
					// $('<li>'+(this.settings.row+1)+'排'+this.settings.label+'座</li>')
					// 	.attr('id', 'cart-item-'+this.settings.id)
					// 	.data('seatId', this.settings.id)
					// 	.appendTo($cart);
					//
					// $counter.text(sc.find('selected').length+1);
					// $total.text(recalculateTotal(sc)+price);

					//console.log('row:'+this.settings.row+1);
					//console.log('label:'+this.settings.label);
					//selectSeats[this.settings.row+1] = this.settings.label;

					//selectSeats.push(this.settings.id);

					return 'selected';
				} else if (this.status() == 'selected') {
					//seat has been vacated
					return 'available';
				} else if (this.status() == 'unavailable') {
					//seat has been already booked
					return 'unavailable';
				} else {
					return this.style();
				}
			}
		});

		//=====这边改的话是直接更改了状态

		//设置锁定的座位，已售出的座位
		//sc.get(['1_2', '4_4','4_5','6_6','6_7','8_5','8_6','8_7','8_8', '10_1', '10_2']).status('unavailable');
		//sc.get(dataBase["soldSeat"]).status('unavailable');
		sc.get(dataBase["soldSeat"]).node().css({
			color: '#FFB800'
		});
		sc.get(dataBase["reserveSeat"]).node().css({
			color: '#FF5722'
		});
		//sc.get(dataBase["soldSeat"]).status('seat-available seat-yellow available');

		//不可选座位
		//sc.get(['3_2']).status('seat-disabled');

		//空白区域
		//sc.get(['4_4']).status('seat-blank');

		//已经单独设置的座位，设置对应的座位号(zuo)
		for ( var t in productSeatList) {
			$("#" + productSeatList[t].coordinate).html(productSeatList[t].zuo);
		}

		// 设置座位容器宽度
		interaction.setSeatContainerWidth();

		function initMapData() {
			// 存放售卖的座位;
			dataBase["soldSeat"] = [];
			dataBase["reserveSeat"] = [];
			dataBase["stage"] = [];
			// 存放ID 映射(对应)的座位数据
			//dataBase["idMapSeat"] = {};

			//解析座位列表数据
			for (var i = 0; i < productSeatList.length; i++) {
				var x = productSeatList[i].x;
				var y = productSeatList[i].y;
				// var priceGrade = interaction.getOnlyAppointObj(priceList,
				// 		"ticketPriceId", seatList[i].pid).priceGradeShow;
				// var flagArr = tempArr[y - mapSize.minY].split("");
				// flagArr[x - mapSize.minX] = priceGrade;
				// tempArr[y - mapSize.minY] = flagArr.join("");
				//var seatMapId = (y - mapSize.minY + 1) + "_" + (x - mapSize.minX + 1);
				var seatMapId = x + "_" + y;
				// 判断是否已售
				if (productSeatList[i].status == 2) {
					dataBase["soldSeat"].push(seatMapId);
				}
				console.log('tag:'+productSeatList[i].tag);
				if (productSeatList[i].tag == 'z') { //不可选座位
					dataBase["soldSeat"].push(seatMapId);
				}
				// 判断是否预留
				if (productSeatList[i].status == 3) {
					dataBase["reserveSeat"].push(seatMapId);
				}
				if (productSeatList[i].pid == 0) {
					dataBase["stage"].push(seatMapId);
				}
				//dataBase["idMapSeat"][seatMapId] = productSeatList[i];
			}

			var tempArr = initSeatMap;
			return tempArr;
		}

		//计算总金额
		function recalculateTotal(sc){
			var total = 0;
			sc.find('selected').each(function () {
				total += price;
			});

			return total;
		}
	}
}

// 交互
var interaction = {
	// 设置座位容器宽度
	setSeatContainerWidth : function() {
		var mapMaxSeatCount = dataBase["seatMap"][0].length;
		var seatWidth = $(".seatCharts-cell").outerWidth(true);
		seatWidth = seatWidth;
		mapMaxSeatCount = mapMaxSeatCount + 2;
		console.log('seatWidth:'+seatWidth);
		console.log('mapMaxSeatCount:'+mapMaxSeatCount);
		$("#seat-map").width(mapMaxSeatCount * seatWidth);
		//alert(window.innerWidth / (mapMaxSeatCount * seatWidth));
		// var scale = window.innerWidth / (mapMaxSeatCount * seatWidth);
		// //alert(scale);
		// $('#renderSeat').css({
		// 	transform : 'scale(' + scale + ')'
		// })
	},
};

initData.seatList();

//生成座位图
function generate_grid(){
	var rows = $('#seat-rows').val();
	var cols = $('#seat-cols').val();
	var x = cols;
	var y = rows;
	var map = new Array();
	for(var i=1;i<=x;i++){
		var tmp = '';
		for(var j=1;j<=y;j++){
			tmp += "a";
		}
		map.push(tmp);
	}
	console.log(map);
	dataBase["seatMap"] = map;

	initSeats();
}
//设置座位状态
function mark_seats(){
	//获取选中的座位
	//获取已选择的座位，原先用selectSeats数组，但是感觉不太准确
	var selectSeats = [];
	var seatCell = $(".seatCharts-cell");
	seatCell.each(function() {
		var mapId = $(this).attr("id");
		var checkFlag = $(this).hasClass("selected");
		if(checkFlag == true){
			selectSeats.push(mapId);
		}
	})

	var seatStatus = $("input[name='seat-status']:checked").val();
	if(!seatStatus){
		return;
	}

	var x = initCols;
	var y = initRows;

	//尝试循环seatMap
	var oriMapArr = dataBase["seatMap"];
	console.log(oriMapArr);
	var newMapArr = new Array();
	for(var i=1;i<=x;i++){
		console.log(i)
		var colStr = oriMapArr[i-1];
		var colArr = colStr.split('');
		var tmpStr = '';
		for(var j=1;j<=y;j++){
			if(seatStatus){
				if(selectSeats.indexOf(i+'_'+j) == -1){
					tmpStr += colArr[j-1];
				}else{
					tmpStr += seatStatus;
				}
			}
		}
		//console.log(tmpStr);
		newMapArr.push(tmpStr);
	}
	//console.log(newMapArr);
	dataBase["seatMap"] = newMapArr;

	//更新数据库
	var param = {
		pid : pid,
		specId : specId,
		rows : initRows,
		cols : initCols,
		seatStatus : seatStatus
	};

	param.selectSeats = JSON.stringify(selectSeats);
	console.log(param);

	$.post(url_saveSeatsStatus, param, function(ret){
		if(ret.code == 1){
			Wei.Util.wapTipNew({msg: ret.msg, icon: 1, data: ret.data}, function(){
				layer.closeAll();
			});
			//initData.seatList();
		}else if(ret.code == 0){
			Wei.Util.wapTipNew({msg: ret.msg, icon: 2, data: ret.data});
		}
	},'json');

	// var map = new Array();
	// var seatCell = $(".seatCharts-cell");
	// seatCell.each(function() {
	// 	var mapId = $(this).attr("id");
	//
	// 	var checkFlag = $(this).hasClass("seat-available");
	// 	//console.log('checkFlag:'+checkFlag)
	// 	if(checkFlag == true){
	// 		seatData1.push(mapId);
	// 	}
	//
	// 	var checkFlag = $(this).hasClass("seat-disabled");
	// 	//console.log('checkFlag:'+checkFlag)
	// 	if(checkFlag == true){
	// 		seatData2.push(mapId);
	// 	}
	//
	// 	var checkFlag = $(this).hasClass("seat-blank");
	// 	//console.log('checkFlag:'+checkFlag)
	// 	if(checkFlag == true){
	// 		seatData3.push(mapId);
	// 	}
	// })
	// dataBase["seatMap"] = map;

	//使用横排数列来循环
	// var rows = $('#seat-rows').val();
	// var cols = $('#seat-cols').val();
	// var x = cols;
	// var y = rows;
	// var map = new Array();
	// for(var i=1;i<=x;i++){
	// 	var tmp = "";
	// 	for(var j=1;j<=y;j++){
	// 		if(stype == 'seat-disabled'){
	// 			if(selectSeats.indexOf(i+'_'+j) == -1){
	// 				console.log('1');
	// 				tmp += "a";
	// 			}else{
	// 				console.log('2');
	// 				tmp += "d";
	// 			}
	// 		}else if(stype == 'seat-blank'){
	// 			if(selectSeats.indexOf(i+'_'+j) == -1){
	// 				console.log('3');
	// 				tmp += "a";
	// 			}else{
	// 				console.log('4');
	// 				tmp += "b";
	// 			}
	// 		}else{
	// 			console.log('5');
	// 			tmp += "a";
	// 		}
	// 	}
	// 	map.push(tmp);
	// }
	// dataBase["seatMap"] = map;

	// var map = [];
	// for(var i=0;i<x;i++){
	// 	map[i]="";
	// 	for(var j=0;j<y;j++){
	// 		if(stype == 'no-seats'){
	// 			var tmpI = i+1;
	// 			var tmpJ = j+1;
	// 			if(selectSeats.indexOf(tmpI+'_'+tmpJ) == -1){
	// 				console.log('1');
	// 				map[i]+="a";
	// 			}else{
	// 				console.log('2');
	// 				map[i]+="_";
	// 			}
	// 		}else{
	// 			console.log('3');
	// 			map[i]+="a";
	// 		}
	// 	}
	// }
}